<!DOCTYPE html>
<html lang="zh_CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.45">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"><title>科科人神</title><meta name="description" content="科科人神，后端软件工程师，喜欢云原生">
    <link rel="modulepreload" href="/assets/app.80e5dc69.js"><link rel="modulepreload" href="/assets/index.html.2275c56f.js"><link rel="modulepreload" href="/assets/index.html.2f735cf1.js"><link rel="prefetch" href="/assets/index.html.cba988ab.js"><link rel="prefetch" href="/assets/1.html.8ac90e92.js"><link rel="prefetch" href="/assets/19.html.4444be95.js"><link rel="prefetch" href="/assets/必备知识.html.7da85c42.js"><link rel="prefetch" href="/assets/必备知识罗列.html.5e18db77.js"><link rel="prefetch" href="/assets/1.html.356f0755.js"><link rel="prefetch" href="/assets/2.html.085440fc.js"><link rel="prefetch" href="/assets/3.html.09effb5c.js"><link rel="prefetch" href="/assets/1.html.50fb5135.js"><link rel="prefetch" href="/assets/1.html.17222973.js"><link rel="prefetch" href="/assets/1.html.0a197ef1.js"><link rel="prefetch" href="/assets/4-1.html.073fbd99.js"><link rel="prefetch" href="/assets/4-2.html.64039343.js"><link rel="prefetch" href="/assets/4.html.021c0881.js"><link rel="prefetch" href="/assets/5-6-1.html.34978ed1.js"><link rel="prefetch" href="/assets/5-6-2.html.f004f560.js"><link rel="prefetch" href="/assets/5-6.html.f5ad3835.js"><link rel="prefetch" href="/assets/index.html.467e6eba.js"><link rel="prefetch" href="/assets/index.html.f3622043.js"><link rel="prefetch" href="/assets/index.html.a2792a89.js"><link rel="prefetch" href="/assets/index.html.5d63961d.js"><link rel="prefetch" href="/assets/index.html.0b5fc023.js"><link rel="prefetch" href="/assets/index.html.9d71a6d6.js"><link rel="prefetch" href="/assets/bash_completions.html.ed68147e.js"><link rel="prefetch" href="/assets/man_docs.html.2f0dd0c9.js"><link rel="prefetch" href="/assets/md_docs.html.20de452a.js"><link rel="prefetch" href="/assets/rest_docs.html.b77e8a2e.js"><link rel="prefetch" href="/assets/zsh_completions.html.60976ab2.js"><link rel="prefetch" href="/assets/syslog.html.48554683.js"><link rel="prefetch" href="/assets/daily_dep.html.2f155f5c.js"><link rel="prefetch" href="/assets/getting_started.html.769a2eaa.js"><link rel="prefetch" href="/assets/installation.html.e6b0002b.js"><link rel="prefetch" href="/assets/migrating.html.128871c1.js"><link rel="prefetch" href="/assets/new_project.html.8326d390.js"><link rel="prefetch" href="/assets/uninstalling.html.26650652.js"><link rel="prefetch" href="/assets/deduction.html.d4acd3fa.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.603c62de.js"><link rel="prefetch" href="/assets/404.html.a15b4cbd.js"><link rel="prefetch" href="/assets/index.html.29aac47c.js"><link rel="prefetch" href="/assets/index.html.b64fbb23.js"><link rel="prefetch" href="/assets/index.html.cd48e230.js"><link rel="prefetch" href="/assets/index.html.6b048e76.js"><link rel="prefetch" href="/assets/index.html.7f0a6e54.js"><link rel="prefetch" href="/assets/index.html.b5a71586.js"><link rel="prefetch" href="/assets/index.html.60805430.js"><link rel="prefetch" href="/assets/index.html.60736b0b.js"><link rel="prefetch" href="/assets/index.html.80dddd6d.js"><link rel="prefetch" href="/assets/index.html.cc3c6a0d.js"><link rel="prefetch" href="/assets/index.html.44e62fda.js"><link rel="prefetch" href="/assets/index.html.015a0b2c.js"><link rel="prefetch" href="/assets/index.html.415d4517.js"><link rel="prefetch" href="/assets/index.html.d1d65e23.js"><link rel="prefetch" href="/assets/index.html.725ac526.js"><link rel="prefetch" href="/assets/index.html.c824ee30.js"><link rel="prefetch" href="/assets/1.html.4fb25663.js"><link rel="prefetch" href="/assets/19.html.ded8a9c9.js"><link rel="prefetch" href="/assets/必备知识.html.0ff3a77d.js"><link rel="prefetch" href="/assets/必备知识罗列.html.85e0f635.js"><link rel="prefetch" href="/assets/1.html.4f76037b.js"><link rel="prefetch" href="/assets/2.html.05a6e91e.js"><link rel="prefetch" href="/assets/3.html.30d4c4b6.js"><link rel="prefetch" href="/assets/1.html.ce86a757.js"><link rel="prefetch" href="/assets/1.html.0f8a9434.js"><link rel="prefetch" href="/assets/1.html.f4ee2fe7.js"><link rel="prefetch" href="/assets/4-1.html.c7cc75b6.js"><link rel="prefetch" href="/assets/4-2.html.36807608.js"><link rel="prefetch" href="/assets/4.html.c03ae622.js"><link rel="prefetch" href="/assets/5-6-1.html.651e5517.js"><link rel="prefetch" href="/assets/5-6-2.html.5ccfdb5e.js"><link rel="prefetch" href="/assets/5-6.html.882bc951.js"><link rel="prefetch" href="/assets/index.html.9d9ba895.js"><link rel="prefetch" href="/assets/index.html.3da373d1.js"><link rel="prefetch" href="/assets/index.html.6ec0b380.js"><link rel="prefetch" href="/assets/index.html.f8c39be7.js"><link rel="prefetch" href="/assets/index.html.e8d4dc57.js"><link rel="prefetch" href="/assets/index.html.711c721b.js"><link rel="prefetch" href="/assets/bash_completions.html.31fa6426.js"><link rel="prefetch" href="/assets/man_docs.html.cc0f68c6.js"><link rel="prefetch" href="/assets/md_docs.html.dd10674f.js"><link rel="prefetch" href="/assets/rest_docs.html.46da86e5.js"><link rel="prefetch" href="/assets/zsh_completions.html.64d57b81.js"><link rel="prefetch" href="/assets/syslog.html.5324d692.js"><link rel="prefetch" href="/assets/daily_dep.html.b7a8b70b.js"><link rel="prefetch" href="/assets/getting_started.html.29c6534a.js"><link rel="prefetch" href="/assets/installation.html.b11da95a.js"><link rel="prefetch" href="/assets/migrating.html.92825adc.js"><link rel="prefetch" href="/assets/new_project.html.8fcfe511.js"><link rel="prefetch" href="/assets/uninstalling.html.34c1a6fb.js"><link rel="prefetch" href="/assets/deduction.html.2f0a618b.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.7818d8b5.js"><link rel="prefetch" href="/assets/404.html.37719150.js"><link rel="prefetch" href="/assets/index.html.bc2fbcdd.js"><link rel="prefetch" href="/assets/index.html.2363e42b.js"><link rel="prefetch" href="/assets/index.html.691f6b43.js"><link rel="prefetch" href="/assets/index.html.c73599f4.js"><link rel="prefetch" href="/assets/index.html.32559aa6.js"><link rel="prefetch" href="/assets/index.html.283a60cc.js"><link rel="prefetch" href="/assets/index.html.b72bddc9.js"><link rel="prefetch" href="/assets/index.html.aebed871.js"><link rel="prefetch" href="/assets/index.html.1bace120.js"><link rel="prefetch" href="/assets/index.html.7913d44a.js"><link rel="prefetch" href="/assets/index.html.4e5360d4.js"><link rel="prefetch" href="/assets/index.html.028748b8.js"><link rel="prefetch" href="/assets/index.html.e9cb3a86.js"><link rel="prefetch" href="/assets/index.html.e31e8159.js"><link rel="prefetch" href="/assets/index.html.01049197.js"><link rel="prefetch" href="/assets/404.30cc8863.js"><link rel="prefetch" href="/assets/HomePage.4e823a0b.js"><link rel="prefetch" href="/assets/Layout.61f5e0fd.js"><link rel="prefetch" href="/assets/Links.8680cda6.js"><link rel="prefetch" href="/assets/Post.93b09438.js"><link rel="prefetch" href="/assets/Tags.3d9bb337.js">
    <link rel="stylesheet" href="/assets/style.8b24233a.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar is-fixed is-visible invert"><span><a href="/" class=""><span class="site-name">$ cd /home/</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading"> <!----></p><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/resty/#news" class="router-link-active router-link-exact-active sidebar-item" aria-label="News"><!--[--><!--]--><!----><span>News</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#features" class="router-link-active router-link-exact-active sidebar-item" aria-label="Features"><!--[--><!--]--><!----><span>Features</span><!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/resty/#included-batteries" class="router-link-active router-link-exact-active sidebar-item" aria-label="Included Batteries"><!--[--><!--]--><!----><span>Included Batteries</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#it-might-be-beneficial-for-your-project" class="router-link-active router-link-exact-active sidebar-item" aria-label="It might be beneficial for your project 😄"><!--[--><!--]--><!----><span>It might be beneficial for your project 😄</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#installation" class="router-link-active router-link-exact-active sidebar-item" aria-label="Installation"><!--[--><!--]--><!----><span>Installation</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#usage" class="router-link-active router-link-exact-active sidebar-item" aria-label="Usage"><!--[--><!--]--><!----><span>Usage</span><!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/resty/#multipart-file-s-upload" class="router-link-active router-link-exact-active sidebar-item" aria-label="Multipart File(s) upload"><!--[--><!--]--><!----><span>Multipart File(s) upload</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#versioning" class="router-link-active router-link-exact-active sidebar-item" aria-label="Versioning"><!--[--><!--]--><!----><span>Versioning</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#contribution" class="router-link-active router-link-exact-active sidebar-item" aria-label="Contribution"><!--[--><!--]--><!----><span>Contribution</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#creator" class="router-link-active router-link-exact-active sidebar-item" aria-label="Creator"><!--[--><!--]--><!----><span>Creator</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#contributors" class="router-link-active router-link-exact-active sidebar-item" aria-label="Contributors"><!--[--><!--]--><!----><span>Contributors</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/resty/#license" class="router-link-active router-link-exact-active sidebar-item" aria-label="License"><!--[--><!--]--><!----><span>License</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul><!--[--><!--]--></aside><!--]--><div class="page-content"><!--[--><main class="page"><!--[--><div class="article-header" style="display:none;"><!----><div class="article-header-content"><!----><h1 class="article-title"></h1><!----><div class="article-icons"><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M313.6 304c-28.7 0-42.5 16-89.6 16-47.1 0-60.8-16-89.6-16C60.2 304 0 364.2 0 438.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-25.6c0-74.2-60.2-134.4-134.4-134.4zM400 464H48v-25.6c0-47.6 38.8-86.4 86.4-86.4 14.6 0 38.3 16 89.6 16 51.7 0 74.9-16 89.6-16 47.6 0 86.4 38.8 86.4 86.4V464zM224 288c79.5 0 144-64.5 144-144S303.5 0 224 0 80 64.5 80 144s64.5 144 144 144zm0-240c52.9 0 96 43.1 96 96s-43.1 96-96 96-96-43.1-96-96 43.1-96 96-96z"/></svg><span>科科人神</span></div><!----><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M17.618 5.968l1.453-1.453 1.414 1.414-1.453 1.453a9 9 0 11-1.414-1.414zM12 20a7 7 0 100-14 7 7 0 000 14zM11 8h2v6h-2V8zM8 1h8v2H8V1z"/></svg><span>24 min</span></div></div></div><!----></div><!--[--><!--]--><!--]--><div class="theme-gungnir-content"><!--[--><!--]--><!--[--><p align="center"><h1 align="center">Resty 中文文档</h1><p align="center">Go 的简单的 HTTP 和 REST 客户端库 (灵感来源于 Ruby 的 rest-client)</p><p align="center"><a href="#features">Features</a> 详细描述了 Resty 的功能</p></p><p align="center"><p align="center"><a href="https://travis-ci.org/go-resty/resty"><img src="https://travis-ci.org/go-resty/resty.svg?branch=master" alt="Build Status"></a><a href="https://codecov.io/gh/go-resty/resty/branch/master"><img src="https://codecov.io/gh/go-resty/resty/branch/master/graph/badge.svg" alt="Code Coverage"></a><a href="https://goreportcard.com/report/go-resty/resty"><img src="https://goreportcard.com/badge/go-resty/resty" alt="Go Report Card"></a><a href="https://github.com/go-resty/resty/releases/latest"><img src="https://img.shields.io/badge/version-2.0.0-blue.svg" alt="Release Version"></a><a href="https://godoc.org/github.com/go-resty/resty"><img src="https://godoc.org/github.com/go-resty/resty?status.svg" alt="GoDoc"></a><a href="LICENSE"><img src="https://img.shields.io/github/license/go-resty/resty.svg" alt="License"></a></p></p><p align="center"><h4 align="center">Resty Communication Channels</h4><p align="center"><a href="https://gitter.im/go_resty/community?utm_source=badge&amp;utm_medium=badge&amp;utm_campaign=pr-badge&amp;utm_content=badge"><img src="https://badges.gitter.im/go_resty/community.svg" alt="Chat on Gitter - Resty Community"></a><a href="https://twitter.com/go_resty"><img src="https://img.shields.io/badge/twitter-@go__resty-55acee.svg" alt="Twitter @go_resty"></a></p></p><h2 id="news" tabindex="-1"><a class="header-anchor" href="#news" aria-hidden="true">#</a> News</h2><ul><li>v2.0.0 <a href="https://github.com/go-resty/resty/releases/tag/v2.0.0" target="_blank" rel="noopener noreferrer">released</a> and tagged on Jul 16, 2019.</li><li>v1.12.0 <a href="https://github.com/go-resty/resty/releases/tag/v1.12.0" target="_blank" rel="noopener noreferrer">released</a> and tagged on Feb 27, 2019.</li><li>v1.0 released and tagged on Sep 25, 2017. - Resty&#39;s first version was released on Sep 15, 2015 then it grew gradually as a very handy and helpful library. Its been a two years since first release. I&#39;m very thankful to Resty users and its <a href="https://github.com/go-resty/resty/graphs/contributors" target="_blank" rel="noopener noreferrer">contributors</a>.</li></ul><h2 id="features" tabindex="-1"><a class="header-anchor" href="#features" aria-hidden="true">#</a> Features</h2><ul><li>GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.</li><li>Simple and chainable methods for settings and request</li><li>Request Body can be <code>string</code>, <code>[]byte</code>, <code>struct</code>, <code>map</code>, <code>slice</code> and <code>io.Reader</code> too <ul><li>Auto detects <code>Content-Type</code></li><li>Buffer less processing for <code>io.Reader</code></li></ul></li><li><a href="https://godoc.org/github.com/go-resty/resty#Response" target="_blank" rel="noopener noreferrer">Response</a> object gives you more possibility <ul><li>Access as <code>[]byte</code> array - <code>response.Body()</code> OR Access as <code>string</code> - <code>response.String()</code></li><li>Know your <code>response.Time()</code> and when we <code>response.ReceivedAt()</code></li></ul></li><li>Automatic marshal and unmarshal for <code>JSON</code> and <code>XML</code> content type <ul><li>Default is <code>JSON</code>, if you supply <code>struct/map</code> without header <code>Content-Type</code></li><li>For auto-unmarshal, refer to - <ul><li>Success scenario <a href="https://godoc.org/github.com/go-resty/resty#Request.SetResult" target="_blank" rel="noopener noreferrer">Request.SetResult()</a> and <a href="https://godoc.org/github.com/go-resty/resty#Response.Result" target="_blank" rel="noopener noreferrer">Response.Result()</a>.</li><li>Error scenario <a href="https://godoc.org/github.com/go-resty/resty#Request.SetError" target="_blank" rel="noopener noreferrer">Request.SetError()</a> and <a href="https://godoc.org/github.com/go-resty/resty#Response.Error" target="_blank" rel="noopener noreferrer">Response.Error()</a>.</li><li>Supports <a href="https://tools.ietf.org/html/rfc7807" target="_blank" rel="noopener noreferrer">RFC7807</a> - <code>application/problem+json</code> &amp; <code>application/problem+xml</code></li></ul></li></ul></li><li>Easy to upload one or more file(s) via <code>multipart/form-data</code><ul><li>Auto detects file content type</li></ul></li><li>Request URL <a href="https://godoc.org/github.com/go-resty/resty#Request.SetPathParams" target="_blank" rel="noopener noreferrer">Path Params (aka URI Params)</a></li><li>Backoff Retry Mechanism with retry condition function <a href="retry_test.go">reference</a></li><li>Resty client HTTP &amp; REST <a href="https://godoc.org/github.com/go-resty/resty#Client.OnBeforeRequest" target="_blank" rel="noopener noreferrer">Request</a> and <a href="https://godoc.org/github.com/go-resty/resty#Client.OnAfterResponse" target="_blank" rel="noopener noreferrer">Response</a> middlewares</li><li><code>Request.SetContext</code> supported</li><li>Authorization option of <code>BasicAuth</code> and <code>Bearer</code> token</li><li>Set request <code>ContentLength</code> value for all request or particular request</li><li>Custom <a href="https://godoc.org/github.com/go-resty/resty#Client.SetRootCertificate" target="_blank" rel="noopener noreferrer">Root Certificates</a> and Client <a href="https://godoc.org/github.com/go-resty/resty#Client.SetCertificates" target="_blank" rel="noopener noreferrer">Certificates</a></li><li>Download/Save HTTP response directly into File, like <code>curl -o</code> flag. See <a href="https://godoc.org/github.com/go-resty/resty#Client.SetOutputDirectory" target="_blank" rel="noopener noreferrer">SetOutputDirectory</a> &amp; <a href="https://godoc.org/github.com/go-resty/resty#Request.SetOutput" target="_blank" rel="noopener noreferrer">SetOutput</a>.</li><li>Cookies for your request and CookieJar support</li><li>SRV Record based request instead of Host URL</li><li>Client settings like <code>Timeout</code>, <code>RedirectPolicy</code>, <code>Proxy</code>, <code>TLSClientConfig</code>, <code>Transport</code>, etc.</li><li>Optionally allows GET request with payload, see <a href="https://godoc.org/github.com/go-resty/resty#Client.SetAllowGetMethodPayload" target="_blank" rel="noopener noreferrer">SetAllowGetMethodPayload</a></li><li>Supports registering external JSON library into resty, see <a href="https://github.com/go-resty/resty/issues/76#issuecomment-314015250" target="_blank" rel="noopener noreferrer">how to use</a></li><li>Exposes Response reader without reading response (no auto-unmarshaling) if need be, see <a href="https://github.com/go-resty/resty/issues/87#issuecomment-322100604" target="_blank" rel="noopener noreferrer">how to use</a></li><li>Option to specify expected <code>Content-Type</code> when response <code>Content-Type</code> header missing. Refer to <a href="https://github.com/go-resty/resty/issues/92" target="_blank" rel="noopener noreferrer">#92</a></li><li>Resty design <ul><li>Have client level settings &amp; options and also override at Request level if you want to</li><li>Request and Response middlewares</li><li>Create Multiple clients if you want to <code>resty.New()</code></li><li>Supports <code>http.RoundTripper</code> implementation, see <a href="https://godoc.org/github.com/go-resty/resty#Client.SetTransport" target="_blank" rel="noopener noreferrer">SetTransport</a></li><li>goroutine concurrent safe</li><li>Resty Client trace, see <a href="https://godoc.org/github.com/go-resty/resty#Client.EnableTrace" target="_blank" rel="noopener noreferrer">Client.EnableTrace</a> and <a href="https://godoc.org/github.com/go-resty/resty#Request.EnableTrace" target="_blank" rel="noopener noreferrer">Request.EnableTrace</a></li><li>Debug mode - clean and informative logging presentation</li><li>Gzip - Go does it automatically also resty has fallback handling too</li><li>Works fine with <code>HTTP/2</code> and <code>HTTP/1.1</code></li></ul></li><li><a href="#bazel-support">Bazel support</a></li><li>Easily mock Resty for testing, <a href="#mocking-http-requests-using-httpmock-library">for e.g.</a></li><li>Well tested client library</li></ul><h3 id="included-batteries" tabindex="-1"><a class="header-anchor" href="#included-batteries" aria-hidden="true">#</a> Included Batteries</h3><ul><li>重定向策略 - 如何使用查看 <a href="#redirect-policy">这里</a><ul><li>NoRedirectPolicy</li><li>FlexibleRedirectPolicy</li><li>DomainCheckRedirectPolicy</li><li>等等。 <a href="redirect.go">更多信息</a></li></ul></li><li>重试机制，如何使用查看 <a href="#retries">这里</a><ul><li>Backoff Retry</li><li>Conditional Retry</li></ul></li><li>SRV Record based request instead of Host URL <a href="resty_test.go#L1412">how to use</a></li><li>etc (upcoming - throw your idea&#39;s <a href="https://github.com/go-resty/resty/issues" target="_blank" rel="noopener noreferrer">here</a>).</li></ul><h4 id="supported-go-versions" tabindex="-1"><a class="header-anchor" href="#supported-go-versions" aria-hidden="true">#</a> Supported Go Versions</h4><p>最初，Resty 从 <code>v1.10.0</code> 版本开始支持<code>go modules</code>。</p><p>使用 Resty v2 和更高的版本, 它完全包含 <a href="https://github.com/golang/go/wiki/Modules" target="_blank" rel="noopener noreferrer">go modules</a> 包版本.。</p><p>它需要一个能够理解 <code>/vN</code> 后缀导入的 Go 版本：</p><ul><li>1.9.7+</li><li>1.10.3+</li><li>1.11+</li></ul><h2 id="it-might-be-beneficial-for-your-project" tabindex="-1"><a class="header-anchor" href="#it-might-be-beneficial-for-your-project" aria-hidden="true">#</a> It might be beneficial for your project 😄</h2><p>Resty 的作者还为 Go 发布了以下项目。</p><ul><li><a href="https://aahframework.org" target="_blank" rel="noopener noreferrer">aah framework</a> - 一个安全、灵活、快速的 Go web 框架。</li><li><a href="https://thumbai.app" target="_blank" rel="noopener noreferrer">THUMBAI</a> - Go Mod 仓库, Go Vanity 服务和简单的代理服务器。</li><li><a href="https://github.com/jeevatkm/go-model" target="_blank" rel="noopener noreferrer">go-model</a> - 健壮的，易于使用的 Go <code>struct</code> 模型映射器和工具方法。</li></ul><h2 id="installation" tabindex="-1"><a class="header-anchor" href="#installation" aria-hidden="true">#</a> Installation</h2><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># Go Modules</span>
require github.com/go-resty/resty/v2 v2.0.0
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="usage" tabindex="-1"><a class="header-anchor" href="#usage" aria-hidden="true">#</a> Usage</h2><p>下面的示例会帮助你熟悉 resty 库。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 导入 resty</span>
<span class="token keyword">import</span> <span class="token string">&quot;github.com/go-resty/resty/v2&quot;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="simple-get" tabindex="-1"><a class="header-anchor" href="#simple-get" aria-hidden="true">#</a> Simple GET</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
		<span class="token function">EnableTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
		<span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;https://httpbin.org/get&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// Explore response object</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Response Info:&quot;</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Error      :&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Status Code:&quot;</span><span class="token punctuation">,</span> resp<span class="token punctuation">.</span><span class="token function">StatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Status     :&quot;</span><span class="token punctuation">,</span> resp<span class="token punctuation">.</span><span class="token function">Status</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Time       :&quot;</span><span class="token punctuation">,</span> resp<span class="token punctuation">.</span><span class="token function">Time</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Received At:&quot;</span><span class="token punctuation">,</span> resp<span class="token punctuation">.</span><span class="token function">ReceivedAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Body       :\n&quot;</span><span class="token punctuation">,</span> resp<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// Explore trace info</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Request Trace Info:&quot;</span><span class="token punctuation">)</span>
ti <span class="token operator">:=</span> resp<span class="token punctuation">.</span>Request<span class="token punctuation">.</span><span class="token function">TraceInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;DNSLookup    :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>DNSLookup<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;ConnTime     :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>ConnTime<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;TLSHandshake :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>TLSHandshake<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;ServerTime   :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>ServerTime<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;ResponseTime :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>ResponseTime<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;TotalTime    :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>TotalTime<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;IsConnReused :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>IsConnReused<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;IsConnWasIdle:&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>IsConnWasIdle<span class="token punctuation">)</span>
fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;ConnIdleTime :&quot;</span><span class="token punctuation">,</span> ti<span class="token punctuation">.</span>ConnIdleTime<span class="token punctuation">)</span>

<span class="token comment">/* Output
Response Info:
Error      : &lt;nil&gt;
Status Code: 200
Status     : 200 OK
Time       : 465.301137ms
Received At: 2019-03-10 01:52:33.772456 -0800 PST m=+0.466672260
Body       :
 {
  &quot;args&quot;: {},
  &quot;headers&quot;: {
    &quot;Accept-Encoding&quot;: &quot;gzip&quot;,
    &quot;Host&quot;: &quot;httpbin.org&quot;,
    &quot;User-Agent&quot;: &quot;go-resty/2.0.0-rc.1 (https://github.com/go-resty/resty)&quot;
  },
  &quot;origin&quot;: &quot;0.0.0.0&quot;,
  &quot;url&quot;: &quot;https://httpbin.org/get&quot;
}

Request Trace Info:
DNSLookup    : 2.21124ms
ConnTime     : 393.875795ms
TLSHandshake : 319.313546ms
ServerTime   : 71.109256ms
ResponseTime : 94.466µs
TotalTime    : 465.301137ms
IsConnReused : false
IsConnWasIdle: false
ConnIdleTime : 0s
*/</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="enhanced-get" tabindex="-1"><a class="header-anchor" href="#enhanced-get" aria-hidden="true">#</a> Enhanced GET</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetQueryParams</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
          <span class="token string">&quot;page_no&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span>
          <span class="token string">&quot;limit&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;20&quot;</span><span class="token punctuation">,</span>
          <span class="token string">&quot;sort&quot;</span><span class="token punctuation">:</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span>
          <span class="token string">&quot;order&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;asc&quot;</span><span class="token punctuation">,</span>
          <span class="token string">&quot;random&quot;</span><span class="token punctuation">:</span>strconv<span class="token punctuation">.</span><span class="token function">FormatInt</span><span class="token punctuation">(</span>time<span class="token punctuation">.</span><span class="token function">Now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Unix</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetHeader</span><span class="token punctuation">(</span><span class="token string">&quot;Accept&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/search_result&quot;</span><span class="token punctuation">)</span>


<span class="token comment">// 使用 Request.SetQueryString 方法示例</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetQueryString</span><span class="token punctuation">(</span><span class="token string">&quot;productId=232&amp;template=fresh-sample&amp;cat=resty&amp;source=google&amp;kw=buy a lot more&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetHeader</span><span class="token punctuation">(</span><span class="token string">&quot;Accept&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/show_product&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="various-post-method-combinations" tabindex="-1"><a class="header-anchor" href="#various-post-method-combinations" aria-hidden="true">#</a> Various POST method combinations</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// POST JSON string</span>
<span class="token comment">// 不需要设置 content type, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetHeader</span><span class="token punctuation">(</span><span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span><span class="token string">`{&quot;username&quot;:&quot;testuser&quot;, &quot;password&quot;:&quot;testpass&quot;}`</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetResult</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>AuthSuccess<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>    <span class="token comment">// or SetResult(AuthSuccess{}).</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/login&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// POST []byte array</span>
<span class="token comment">// 不需要设置 content type, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetHeader</span><span class="token punctuation">(</span><span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">`{&quot;username&quot;:&quot;testuser&quot;, &quot;password&quot;:&quot;testpass&quot;}`</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetResult</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>AuthSuccess<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>    <span class="token comment">// or SetResult(AuthSuccess{}).</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/login&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// POST Struct, 默认为 JSON content type。不需要设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span>User<span class="token punctuation">{</span>Username<span class="token punctuation">:</span> <span class="token string">&quot;testuser&quot;</span><span class="token punctuation">,</span> Password<span class="token punctuation">:</span> <span class="token string">&quot;testpass&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetResult</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>AuthSuccess<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>    <span class="token comment">// or SetResult(AuthSuccess{}).</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>AuthError<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(AuthError{}).</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/login&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// POST Map, 默认为 JSON content type。不需要设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">{</span><span class="token string">&quot;username&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;testuser&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;password&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;testpass&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetResult</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>AuthSuccess<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>    <span class="token comment">// or SetResult(AuthSuccess{}).</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>AuthError<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(AuthError{}).</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/login&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// POST of raw bytes for file upload. For example: upload file to Dropbox</span>
fileBytes<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> ioutil<span class="token punctuation">.</span><span class="token function">ReadFile</span><span class="token punctuation">(</span><span class="token string">&quot;/Users/jeeva/mydocument.pdf&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 这里我们没有设置 ，因为 go-resty 会自动为你检测 content-type</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span>fileBytes<span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetContentLength</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span>          <span class="token comment">// Dropbox expects this value</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;your-auth-token&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>DropboxError<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(DropboxError{}).</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;https://content.dropboxapi.com/1/files_put/auto/resty/mydocument.pdf&quot;</span><span class="token punctuation">)</span> <span class="token comment">// for upload Dropbox supports PUT too</span>

<span class="token comment">// 注意: 如果 content-type 的 header, resty 检测请求 body/payload 的 Content-Type.</span>
<span class="token comment">//   * 对于 struct 和 map 数据类型默认为 &#39;application/json&#39;</span>
<span class="token comment">//   * Fallback is plain text content type</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="sample-put" tabindex="-1"><a class="header-anchor" href="#sample-put" aria-hidden="true">#</a> Sample PUT</h4><p>你可以像使用 <code>POST</code> 一样，使用各种组合调用 <code>PUT</code> 方法。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 注意: 这是 PUT 方法使用的一个示例，更多的组合请参考 POST</span>

<span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// Request goes as JSON content type</span>
<span class="token comment">// 不需要设置 auth token, error, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span>Article<span class="token punctuation">{</span>
        Title<span class="token punctuation">:</span> <span class="token string">&quot;go-resty&quot;</span><span class="token punctuation">,</span>
        Content<span class="token punctuation">:</span> <span class="token string">&quot;This is my article content, oh ya!&quot;</span><span class="token punctuation">,</span>
        Author<span class="token punctuation">:</span> <span class="token string">&quot;Jeevanandam M&quot;</span><span class="token punctuation">,</span>
        Tags<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;article&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;sample&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;resty&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Error<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(Error{}).</span>
      <span class="token function">Put</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/article/1234&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="sample-patch" tabindex="-1"><a class="header-anchor" href="#sample-patch" aria-hidden="true">#</a> Sample PATCH</h4><p>你可以像使用 <code>POST</code> 一样，使用各种组合调用 <code>PATCH</code> 方法。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 注意: 这是 PATCH 方法使用的一个示例，更多的组合请参考 POST</span>

<span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// Request goes as JSON content type</span>
<span class="token comment">// 不需要设置 auth token, error, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span>Article<span class="token punctuation">{</span>
        Tags<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;new tag1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;new tag2&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Error<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(Error{}).</span>
      <span class="token function">Patch</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/articles/1234&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="sample-delete-head-options" tabindex="-1"><a class="header-anchor" href="#sample-delete-head-options" aria-hidden="true">#</a> Sample DELETE, HEAD, OPTIONS</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// DELETE a article</span>
<span class="token comment">// 不需要设置 auth token, error, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Error<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(Error{}).</span>
      <span class="token function">Delete</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/articles/1234&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// DELETE a articles with payload/body as a JSON string</span>
<span class="token comment">// 不需要设置 auth token, error, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Error<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>       <span class="token comment">// or SetError(Error{}).</span>
      <span class="token function">SetHeader</span><span class="token punctuation">(</span><span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetBody</span><span class="token punctuation">(</span><span class="token string">`{article_ids: [1002, 1006, 1007, 87683, 45432] }`</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Delete</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/articles&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// HEAD of resource</span>
<span class="token comment">// 不需要设置 auth token, error, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Head</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/videos/hi-res-video&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// OPTIONS of resource</span>
<span class="token comment">// 不需要设置 auth token, error, 如果你有 client level 的设置</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Options</span><span class="token punctuation">(</span><span class="token string">&quot;https://myapp.com/servers/nyc-dc-01&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="multipart-file-s-upload" tabindex="-1"><a class="header-anchor" href="#multipart-file-s-upload" aria-hidden="true">#</a> Multipart File(s) upload</h3><h4 id="using-io-reader" tabindex="-1"><a class="header-anchor" href="#using-io-reader" aria-hidden="true">#</a> Using io.Reader</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>profileImgBytes<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> ioutil<span class="token punctuation">.</span><span class="token function">ReadFile</span><span class="token punctuation">(</span><span class="token string">&quot;/Users/jeeva/test-img.png&quot;</span><span class="token punctuation">)</span>
notesBytes<span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token operator">:=</span> ioutil<span class="token punctuation">.</span><span class="token function">ReadFile</span><span class="token punctuation">(</span><span class="token string">&quot;/Users/jeeva/text-file.txt&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFileReader</span><span class="token punctuation">(</span><span class="token string">&quot;profile_img&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;test-img.png&quot;</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span><span class="token function">NewReader</span><span class="token punctuation">(</span>profileImgBytes<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFileReader</span><span class="token punctuation">(</span><span class="token string">&quot;notes&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;text-file.txt&quot;</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span><span class="token function">NewReader</span><span class="token punctuation">(</span>notesBytes<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFormData</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
          <span class="token string">&quot;first_name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;Jeevanandam&quot;</span><span class="token punctuation">,</span>
          <span class="token string">&quot;last_name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;M&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/upload&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="using-file-directly-from-path" tabindex="-1"><a class="header-anchor" href="#using-file-directly-from-path" aria-hidden="true">#</a> Using File directly from Path</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 上传一个文件</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFile</span><span class="token punctuation">(</span><span class="token string">&quot;profile_img&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;/Users/jeeva/test-img.png&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/upload&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 上传多个文件</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFiles</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
        <span class="token string">&quot;profile_img&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;/Users/jeeva/test-img.png&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;notes&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;/Users/jeeva/text-file.txt&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/upload&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// Multipart of form fields and files</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFiles</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
        <span class="token string">&quot;profile_img&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;/Users/jeeva/test-img.png&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;notes&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;/Users/jeeva/text-file.txt&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFormData</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
        <span class="token string">&quot;first_name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;Jeevanandam&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;last_name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;M&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;zip_code&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;00001&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;city&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;my city&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;access_token&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;C6A79608-782F-4ED0-A11D-BD82FAD829CD&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/profile&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="sample-form-submission" tabindex="-1"><a class="header-anchor" href="#sample-form-submission" aria-hidden="true">#</a> Sample Form submission</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 以 POST 作为一个简单的例子</span>
<span class="token comment">// User Login</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFormData</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
        <span class="token string">&quot;username&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;jeeva&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;password&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;mypass&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/login&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// Followed by profile update</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFormData</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
        <span class="token string">&quot;first_name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;Jeevanandam&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;last_name&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;M&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;zip_code&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;00001&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;city&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;new city update&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/profile&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// Multi value form data</span>
criteria <span class="token operator">:=</span> url<span class="token punctuation">.</span>Values<span class="token punctuation">{</span>
  <span class="token string">&quot;search_criteria&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;book&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;glass&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;pencil&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
resp<span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">SetFormDataFromValues</span><span class="token punctuation">(</span>criteria<span class="token punctuation">)</span><span class="token punctuation">.</span>
      <span class="token function">Post</span><span class="token punctuation">(</span><span class="token string">&quot;http://myapp.com/search&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="save-http-response-into-file" tabindex="-1"><a class="header-anchor" href="#save-http-response-into-file" aria-hidden="true">#</a> Save HTTP Response into File</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 设置要输出的目录路径, 如果目录不存在，go-resty 会创建</span>
<span class="token comment">// 这是一个可选项, 如果你计划在 `Request.SetOutput` 中使用绝对路径，可以一起使用</span>
client<span class="token punctuation">.</span><span class="token function">SetOutputDirectory</span><span class="token punctuation">(</span><span class="token string">&quot;/Users/jeeva/Downloads&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// HTTP 响应保存到文件中, 类似于 curl 的 -o 标志</span>
<span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
          <span class="token function">SetOutput</span><span class="token punctuation">(</span><span class="token string">&quot;plugin/ReplyWithHeader-v5.1-beta.zip&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
          <span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;http://bit.ly/1LouEKr&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 或者使用绝对路径</span>
<span class="token comment">// 注意: 输出目录路径不能用于绝对路径</span>
<span class="token boolean">_</span><span class="token punctuation">,</span> err <span class="token operator">:=</span> client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
          <span class="token function">SetOutput</span><span class="token punctuation">(</span><span class="token string">&quot;/MyDownloads/plugin/ReplyWithHeader-v5.1-beta.zip&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
          <span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;http://bit.ly/1LouEKr&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="request-url-path-params" tabindex="-1"><a class="header-anchor" href="#request-url-path-params" aria-hidden="true">#</a> Request URL Path Params</h4><p>Resty 提供了易于使用的动态请求 URL 路径参数。可以在 client 和 request level 设置参数。 request level 设置的参数会覆盖 client level 的设置的相同参数的值。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetPathParams</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
   <span class="token string">&quot;userId&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;sample@sample.com&quot;</span><span class="token punctuation">,</span>
   <span class="token string">&quot;subAccountId&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;100002&quot;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
<span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/v1/users/{userId}/{subAccountId}/details&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// Result:</span>
<span class="token comment">//   组成的 URL - /v1/users/sample@sample.com/100002/details</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="request-and-response-middleware" tabindex="-1"><a class="header-anchor" href="#request-and-response-middleware" aria-hidden="true">#</a> Request and Response Middleware</h4><p>Resty 提供了处理请求和响应的中间件功能。它比回调函数更灵活。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 注册 Request 中间件</span>
client<span class="token punctuation">.</span><span class="token function">OnBeforeRequest</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>c <span class="token operator">*</span>resty<span class="token punctuation">.</span>Client<span class="token punctuation">,</span> req <span class="token operator">*</span>resty<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
    <span class="token comment">// 现在你可以访问当前 Client 和 Request 对象</span>
    <span class="token comment">// 根据你的需要操作它们 </span>

    <span class="token keyword">return</span> <span class="token boolean">nil</span>  <span class="token comment">// 成功返回 nil，否则返回 error</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 注册 Response 中间件</span>
client<span class="token punctuation">.</span><span class="token function">OnAfterResponse</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>c <span class="token operator">*</span>resty<span class="token punctuation">.</span>Client<span class="token punctuation">,</span> resp <span class="token operator">*</span>resty<span class="token punctuation">.</span>Response<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
    <span class="token comment">// Now you have access to Client and current Response object</span>
    <span class="token comment">// manipulate it as per your need</span>

    <span class="token keyword">return</span> <span class="token boolean">nil</span>  <span class="token comment">// if its success otherwise return error</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="redirect-policy" tabindex="-1"><a class="header-anchor" href="#redirect-policy" aria-hidden="true">#</a> Redirect Policy</h4><p>Resty 提供了一些可以使用重定向策略的选项，而且它支持同时使用多个策略。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 设置 Client 的 Redirect Policy. 根据需要创建一个</span>
client<span class="token punctuation">.</span><span class="token function">SetRedirectPolicy</span><span class="token punctuation">(</span>resty<span class="token punctuation">.</span><span class="token function">FlexibleRedirectPolicy</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">// 想要多个策略，如重定向计数，域名检查等</span>
client<span class="token punctuation">.</span><span class="token function">SetRedirectPolicy</span><span class="token punctuation">(</span>resty<span class="token punctuation">.</span><span class="token function">FlexibleRedirectPolicy</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                        resty<span class="token punctuation">.</span><span class="token function">DomainCheckRedirectPolicy</span><span class="token punctuation">(</span><span class="token string">&quot;host1.com&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;host2.org&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;host3.net&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h5 id="custom-redirect-policy" tabindex="-1"><a class="header-anchor" href="#custom-redirect-policy" aria-hidden="true">#</a> Custom Redirect Policy</h5><p>实现 <a href="redirect.go#L20">RedirectPolicy</a> 接口并且使用 resty client 注册它. 更多内容查看 <a href="redirect.go">redirect.go</a>。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 在 resty.SetRedirectPolicy 使用原始函数</span>
client<span class="token punctuation">.</span><span class="token function">SetRedirectPolicy</span><span class="token punctuation">(</span>resty<span class="token punctuation">.</span><span class="token function">RedirectPolicyFunc</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>req <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">,</span> via <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
  <span class="token comment">// 这里实现你的逻辑</span>

  <span class="token comment">// 继续重定向返回 nil ，否则返回 error 以停止/阻止重定向</span>
  <span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">//---------------------------------------------------</span>

<span class="token comment">// 使用 struct 创建更多的灵活的 redirect policy</span>
<span class="token keyword">type</span> CustomRedirectPolicy <span class="token keyword">struct</span> <span class="token punctuation">{</span>
  <span class="token comment">// variables goes here</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>CustomRedirectPolicy<span class="token punctuation">)</span> <span class="token function">Apply</span><span class="token punctuation">(</span>req <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">,</span> via <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
  <span class="token comment">// 这里实现你的逻辑</span>

  <span class="token comment">// 继续重定向返回 nil ，否则返回 error 以停止/阻止重定向</span>
  <span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token comment">// 在 resty 里注册</span>
client<span class="token punctuation">.</span><span class="token function">SetRedirectPolicy</span><span class="token punctuation">(</span>CustomRedirectPolicy<span class="token punctuation">{</span><span class="token comment">/* initialize variables */</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="custom-root-certificates-and-client-certificates" tabindex="-1"><a class="header-anchor" href="#custom-root-certificates-and-client-certificates" aria-hidden="true">#</a> Custom Root Certificates and Client Certificates</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 自定义的 Root 证书, 只需要提供 .pem 文件</span>
<span class="token comment">// 你可以添加一个或多个 root 证书</span>
client<span class="token punctuation">.</span><span class="token function">SetRootCertificate</span><span class="token punctuation">(</span><span class="token string">&quot;/path/to/root/pemFile1.pem&quot;</span><span class="token punctuation">)</span>
client<span class="token punctuation">.</span><span class="token function">SetRootCertificate</span><span class="token punctuation">(</span><span class="token string">&quot;/path/to/root/pemFile2.pem&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// ... and so on!</span>

<span class="token comment">// 添加 Client 证书, 你可以添加一个或多个证书</span>
<span class="token comment">// 创建 certificate 对象的示例</span>
<span class="token comment">// 从一对文件中解析 public/private key. 这些文件必须包含P EM 编码的数据。</span>
cert1<span class="token punctuation">,</span> err <span class="token operator">:=</span> tls<span class="token punctuation">.</span><span class="token function">LoadX509KeyPair</span><span class="token punctuation">(</span><span class="token string">&quot;certs/client.pem&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;certs/client.key&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
  log<span class="token punctuation">.</span><span class="token function">Fatalf</span><span class="token punctuation">(</span><span class="token string">&quot;ERROR client certificate: %s&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// ...</span>

<span class="token comment">// 添加一个或多个证书</span>
client<span class="token punctuation">.</span><span class="token function">SetCertificates</span><span class="token punctuation">(</span>cert1<span class="token punctuation">,</span> cert2<span class="token punctuation">,</span> cert3<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="proxy-settings-client-as-well-as-at-request-level" tabindex="-1"><a class="header-anchor" href="#proxy-settings-client-as-well-as-at-request-level" aria-hidden="true">#</a> Proxy Settings - Client as well as at Request Level</h4><p>默认情况下 <code>Go</code> 支持从环境变量 <code>HTTP_PROXY</code> 获取代理。Resty 提供了 <code>SetProxy</code> &amp; <code>RemoveProxy</code>， 根据你的需要来选择代理。</p><p><strong>Client Level Proxy</strong> 应用于所有请求的设置</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 设置一个 Proxy URL 和 Port</span>
client<span class="token punctuation">.</span><span class="token function">SetProxy</span><span class="token punctuation">(</span><span class="token string">&quot;http://proxyserver:8888&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 移除 proxy 设置</span>
client<span class="token punctuation">.</span><span class="token function">RemoveProxy</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="retries" tabindex="-1"><a class="header-anchor" href="#retries" aria-hidden="true">#</a> Retries</h4><p>Resty 使用 <a href="http://www.awsarchitectureblog.com/2015/03/backoff.html" target="_blank" rel="noopener noreferrer">backoff</a> 来增加每次尝试后的重试间隔。</p><p>使用示例:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 每个 client 配置重试</span>
client<span class="token punctuation">.</span>
    <span class="token comment">// 将重试计数设置为 非零 以启用重试</span>
    <span class="token function">SetRetryCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
    <span class="token comment">// 你可以覆盖初始的重试等待时间。</span>
    <span class="token comment">// 默认 100 毫秒.</span>
    <span class="token function">SetRetryWaitTime</span><span class="token punctuation">(</span><span class="token number">5</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span><span class="token punctuation">.</span>
    <span class="token comment">// MaxWaitTime 也可以被覆盖.</span>
    <span class="token comment">// 默认是 2 秒.</span>
    <span class="token function">SetRetryMaxWaitTime</span><span class="token punctuation">(</span><span class="token number">20</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span><span class="token punctuation">.</span>
    <span class="token comment">// SetRetryAfter 设置回调函数来计算重试之间的等待时间。</span>
    <span class="token comment">// Default (nil) implies exponential backoff with jitter</span>
    <span class="token function">SetRetryAfter</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>client <span class="token operator">*</span>Client<span class="token punctuation">,</span> resp <span class="token operator">*</span>Response<span class="token punctuation">)</span> <span class="token punctuation">(</span>time<span class="token punctuation">.</span>Duration<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">,</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;quota exceeded&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的设置，当请求返回非 nil 的 error 时，resty 会重试请求 3 次。</p><p>可以选择为 client 提供自定义的重试条件：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

client<span class="token punctuation">.</span><span class="token function">AddRetryCondition</span><span class="token punctuation">(</span>
    <span class="token comment">// RetryConditionFunc type is for retry condition function</span>
	  <span class="token comment">// input: non-nil Response OR request execution error</span>
    <span class="token keyword">func</span><span class="token punctuation">(</span>r <span class="token operator">*</span>resty<span class="token punctuation">.</span>Response<span class="token punctuation">,</span> err <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token builtin">bool</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> r<span class="token punctuation">.</span><span class="token function">StatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> http<span class="token punctuation">.</span>StatusTooManyRequests
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的例子，如果请求以 <code>429 Too Many requests</code> 状态码结束，resty 会重试请求。</p><p>可以添加多个重试条件。</p><p>还可以使用 <code>resty.Backoff(...)</code> 获得任意重试场景的实现。<a href="retry_test.go">Reference</a>。</p><h4 id="allow-get-request-with-payload" tabindex="-1"><a class="header-anchor" href="#allow-get-request-with-payload" aria-hidden="true">#</a> Allow GET request with Payload</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 允许带有 Payload 的 GET 请求。默认是 disabled 的。</span>
client<span class="token punctuation">.</span><span class="token function">SetAllowGetMethodPayload</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="wanna-multiple-clients" tabindex="-1"><a class="header-anchor" href="#wanna-multiple-clients" aria-hidden="true">#</a> Wanna Multiple Clients</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// Here you go!</span>
<span class="token comment">// Client 1</span>
client1 <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
client1<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;http://httpbin.org&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// ...</span>

<span class="token comment">// Client 2</span>
client2 <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
client2<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Head</span><span class="token punctuation">(</span><span class="token string">&quot;http://httpbin.org&quot;</span><span class="token punctuation">)</span>
<span class="token comment">// ...</span>

<span class="token comment">// Bend it as per your need!!!</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="remaining-client-settings-its-options" tabindex="-1"><a class="header-anchor" href="#remaining-client-settings-its-options" aria-hidden="true">#</a> Remaining Client Settings &amp; its Options</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// Client level 的唯一设置</span>
<span class="token comment">//--------------------------------</span>
<span class="token comment">// 启用 debug 模式</span>
client<span class="token punctuation">.</span><span class="token function">SetDebug</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>

<span class="token comment">// 设置 Client TLSClientConfig</span>
<span class="token comment">// 设置自定义的 root-certificate. 参考: http://golang.org/pkg/crypto/tls/#example_Dial</span>
client<span class="token punctuation">.</span><span class="token function">SetTLSClientConfig</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>tls<span class="token punctuation">.</span>Config<span class="token punctuation">{</span> RootCAs<span class="token punctuation">:</span> roots <span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 或者禁用安全检查 (https)</span>
client<span class="token punctuation">.</span><span class="token function">SetTLSClientConfig</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>tls<span class="token punctuation">.</span>Config<span class="token punctuation">{</span> InsecureSkipVerify<span class="token punctuation">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 根据需要设置 client 超时</span>
client<span class="token punctuation">.</span><span class="token function">SetTimeout</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Minute<span class="token punctuation">)</span>


<span class="token comment">// 如果你想，你可以在 request level 覆盖下面的所有的设置和选项</span>
<span class="token comment">//--------------------------------------------------------------------------------</span>
<span class="token comment">// 设置所有请求的 Host URL。就可以在所有请求中使用相对路径</span>
client<span class="token punctuation">.</span><span class="token function">SetHostURL</span><span class="token punctuation">(</span><span class="token string">&quot;http://httpbin.org&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 设置所有请求的 Headers</span>
client<span class="token punctuation">.</span><span class="token function">SetHeader</span><span class="token punctuation">(</span><span class="token string">&quot;Accept&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">)</span>
client<span class="token punctuation">.</span><span class="token function">SetHeaders</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
        <span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;application/json&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;My custom User Agent String&quot;</span><span class="token punctuation">,</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 设置所有请求的 Cookies</span>
client<span class="token punctuation">.</span><span class="token function">SetCookie</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>http<span class="token punctuation">.</span>Cookie<span class="token punctuation">{</span>
      Name<span class="token punctuation">:</span><span class="token string">&quot;go-resty&quot;</span><span class="token punctuation">,</span>
      Value<span class="token punctuation">:</span><span class="token string">&quot;This is cookie value&quot;</span><span class="token punctuation">,</span>
      Path<span class="token punctuation">:</span> <span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span>
      Domain<span class="token punctuation">:</span> <span class="token string">&quot;sample.com&quot;</span><span class="token punctuation">,</span>
      MaxAge<span class="token punctuation">:</span> <span class="token number">36000</span><span class="token punctuation">,</span>
      HttpOnly<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
      Secure<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
client<span class="token punctuation">.</span><span class="token function">SetCookies</span><span class="token punctuation">(</span>cookies<span class="token punctuation">)</span>

<span class="token comment">// 设置所有请求的 URL query parameters</span>
client<span class="token punctuation">.</span><span class="token function">SetQueryParam</span><span class="token punctuation">(</span><span class="token string">&quot;user_id&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;00001&quot;</span><span class="token punctuation">)</span>
client<span class="token punctuation">.</span><span class="token function">SetQueryParams</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span> <span class="token comment">// sample of those who use this manner</span>
      <span class="token string">&quot;api_key&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;api-key-here&quot;</span><span class="token punctuation">,</span>
      <span class="token string">&quot;api_secert&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;api-secert&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetQueryString</span><span class="token punctuation">(</span><span class="token string">&quot;productId=232&amp;template=fresh-sample&amp;cat=resty&amp;source=google&amp;kw=buy a lot more&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 设置所有请求的 Form data. 通常和 POST and PUT 一起</span>
client<span class="token punctuation">.</span><span class="token function">SetFormData</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span>
    <span class="token string">&quot;access_token&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;BC594900-518B-4F7E-AC75-BD37F019E08F&quot;</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">// 设置所有请求的 Basic Auth</span>
client<span class="token punctuation">.</span><span class="token function">SetBasicAuth</span><span class="token punctuation">(</span><span class="token string">&quot;myuser&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;mypass&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 设置所有请求的 Bearer Auth Token</span>
client<span class="token punctuation">.</span><span class="token function">SetAuthToken</span><span class="token punctuation">(</span><span class="token string">&quot;BC594900518B4F7EAC75BD37F019E08FBC594900518B4F7EAC75BD37F019E08F&quot;</span><span class="token punctuation">)</span>

<span class="token comment">// 启用所有请求的 Content length value</span>
client<span class="token punctuation">.</span><span class="token function">SetContentLength</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>

<span class="token comment">// 为 JSON/XML 请求注册全局的 Error 对象结构体</span>
client<span class="token punctuation">.</span><span class="token function">SetError</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Error<span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>    <span class="token comment">// or resty.SetError(Error{})</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unix-socket" tabindex="-1"><a class="header-anchor" href="#unix-socket" aria-hidden="true">#</a> Unix Socket</h4><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>unixSocket <span class="token operator">:=</span> <span class="token string">&quot;/var/run/my_socket.sock&quot;</span>

<span class="token comment">// 创建一个 Go 原生的 http.Transport ，这样可以在 resty 中设置它.</span>
transport <span class="token operator">:=</span> http<span class="token punctuation">.</span>Transport<span class="token punctuation">{</span>
	Dial<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token boolean">_</span><span class="token punctuation">,</span> <span class="token boolean">_</span> <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>net<span class="token punctuation">.</span>Conn<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> net<span class="token punctuation">.</span><span class="token function">Dial</span><span class="token punctuation">(</span><span class="token string">&quot;unix&quot;</span><span class="token punctuation">,</span> unixSocket<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 设置之前创建的 transport, 将通信 scheme 设置为套接字，并将 unixSocket 设置为 HostURL。</span>
client<span class="token punctuation">.</span><span class="token function">SetTransport</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>transport<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetScheme</span><span class="token punctuation">(</span><span class="token string">&quot;http&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SetHostURL</span><span class="token punctuation">(</span>unixSocket<span class="token punctuation">)</span>

<span class="token comment">// 不需要在请求上写主机的 URL，只需要路径。</span>
client<span class="token punctuation">.</span><span class="token function">R</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;/index.html&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="bazel-support" tabindex="-1"><a class="header-anchor" href="#bazel-support" aria-hidden="true">#</a> Bazel support</h4><p>Resty 可以通过 <a href="https://bazel.build" target="_blank" rel="noopener noreferrer">Bazel</a> 构建、测试和依赖 . 例如，运行所有 test:</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>bazel <span class="token builtin class-name">test</span> :go_default_test
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h4 id="mocking-http-requests-using-httpmock-library" tabindex="-1"><a class="header-anchor" href="#mocking-http-requests-using-httpmock-library" aria-hidden="true">#</a> Mocking http requests using <a href="https://github.com/jarcoal/httpmock" target="_blank" rel="noopener noreferrer">httpmock</a> library</h4><p>为了在测试应用程序时模拟 http 请求，可以使用 <code>httpmock</code> 库。 当使用默认 resty client, 要把 client 传递给库，像下面这样:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 创建一个 Resty Client</span>
client <span class="token operator">:=</span> resty<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 获取底层的 HTTP Client，并设置为 Mock</span>
httpmock<span class="token punctuation">.</span><span class="token function">ActivateNonDefault</span><span class="token punctuation">(</span>client<span class="token punctuation">.</span><span class="token function">GetClient</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 ginko 模拟 resty http 请求的更详细示例在 <a href="https://github.com/jarcoal/httpmock#ginkgo--resty-example" target="_blank" rel="noopener noreferrer">这里</a>。</p><h2 id="versioning" tabindex="-1"><a class="header-anchor" href="#versioning" aria-hidden="true">#</a> Versioning</h2><p>Resty releases versions according to <a href="http://semver.org" target="_blank" rel="noopener noreferrer">Semantic Versioning</a></p><ul><li>Resty v2 does not use <code>gopkg.in</code> service for library versioning.</li><li>Resty fully adapted to <code>go mod</code> capabilities since <code>v1.10.0</code> release.</li><li>Resty v1 series was using <code>gopkg.in</code> to provide versioning. <code>gopkg.in/resty.vX</code> points to appropriate tagged versions; <code>X</code> denotes version series number and it&#39;s a stable release for production use. For e.g. <code>gopkg.in/resty.v0</code>.</li><li>Development takes place at the master branch. Although the code in master should always compile and test successfully, it might break API&#39;s. I aim to maintain backwards compatibility, but sometimes API&#39;s and behavior might be changed to fix a bug.</li></ul><h2 id="contribution" tabindex="-1"><a class="header-anchor" href="#contribution" aria-hidden="true">#</a> Contribution</h2><p>I would welcome your contribution! If you find any improvement or issue you want to fix, feel free to send a pull request, I like pull requests that include test cases for fix/enhancement. I have done my best to bring pretty good code coverage. Feel free to write tests.</p><p>BTW, I&#39;d like to know what you think about <code>Resty</code>. Kindly open an issue or send me an email; it&#39;d mean a lot to me.</p><h2 id="creator" tabindex="-1"><a class="header-anchor" href="#creator" aria-hidden="true">#</a> Creator</h2><p><a href="https://github.com/jeevatkm" target="_blank" rel="noopener noreferrer">Jeevanandam M.</a> (jeeva@myjeeva.com)</p><h2 id="contributors" tabindex="-1"><a class="header-anchor" href="#contributors" aria-hidden="true">#</a> Contributors</h2><p>Have a look on <a href="https://github.com/go-resty/resty/graphs/contributors" target="_blank" rel="noopener noreferrer">Contributors</a> page.</p><h2 id="license" tabindex="-1"><a class="header-anchor" href="#license" aria-hidden="true">#</a> License</h2><p>Resty released under MIT license, refer <a href="LICENSE">LICENSE</a> file.</p><!--]--><!--[--><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">Last Updated: </span><!----></div><!----></footer><!----><!--[--><!--]--><!----></main><!--]--></div><div class="search-page" role="search"><span class="search-close"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512" width="28" height="28" fill="currentColor"><path d="M224 416c-8.188 0-16.38-3.125-22.62-9.375l-192-192c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L224 338.8l169.4-169.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-192 192C240.4 412.9 232.2 416 224 416z"></path></svg></span><div class="gungnir-search-box"><input placeholder="$ grep ..." autocomplete="off" spellcheck="false" value><!----></div></div><div class="menu-btn-container"><div class="menu-btn-wrapper"><div class="menu-btn"><div style="" class="menu-btn-icon"><span></span><span></span><span></span></div><div style="display:none;" class="menu-text">0</div><svg class="menu-progress"><circle class="menu-border" cx="50%" cy="50%" r="48%" style="stroke-dasharray:0% 314.15926%;"></circle></svg></div><div class="menu-btn-child-wrapper"><div class="toggle-dark-button menu-btn-child" title="toggle dark mode"><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"/></svg><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 00283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"/></svg><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M224 96l16-32 32-16-32-16-16-32-16 32-32 16 32 16 16 32zM80 160l26.66-53.33L160 80l-53.34-26.67L80 0 53.34 53.33 0 80l53.34 26.67L80 160zm352 128l-26.66 53.33L352 368l53.34 26.67L432 448l26.66-53.33L512 368l-53.34-26.67L432 288zm70.62-193.77L417.77 9.38C411.53 3.12 403.34 0 395.15 0c-8.19 0-16.38 3.12-22.63 9.38L9.38 372.52c-12.5 12.5-12.5 32.76 0 45.25l84.85 84.85c6.25 6.25 14.44 9.37 22.62 9.37 8.19 0 16.38-3.12 22.63-9.37l363.14-363.15c12.5-12.48 12.5-32.75 0-45.24zM359.45 203.46l-50.91-50.91 86.6-86.6 50.91 50.91-86.6 86.6z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.7999999999999998em;" aria-hidden="true" width="28.799999999999997" height="28.799999999999997" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z"/></svg></div><!----><div class="toggle-sidebar-button menu-btn-child menu-btn-sidebar" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M14 2a1 1 0 011 1v10a1 1 0 01-1 1H2a1 1 0 01-1-1V3a1 1 0 011-1h12zM2 1a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V3a2 2 0 00-2-2H2z"/><path d="M3 4a1 1 0 011-1h2a1 1 0 011 1v8a1 1 0 01-1 1H4a1 1 0 01-1-1V4z"/></svg></div></div></div></div><!----></div><!--]--></div>
    <script type="module" src="/assets/app.80e5dc69.js" defer></script>
  </body>
</html>
